﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:MVVM_CAD.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:MVVM_CAD.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

using MVVM_CAD.Common;
using Ninject;

namespace MVVM_CAD.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:MVVM_CAD.ViewModel"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// <para>
    /// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    /// the Main property and bind to the ViewModelNameStatic property instead:
    /// </para>
    /// <code>
    /// xmlns:vm="clr-namespace:MVVM_CAD.ViewModel"
    /// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    /// </code>
    /// </summary>
    public class ViewModelLocator
    {
        private static IKernel _kernel;
        private static MainViewModel _main;
        private static FigurePropertiesViewModel _figureProperties;
        private static ToolsViewModel _tools;

        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view models
            ////}
            ////else
            ////{
            ////    // Create run time view models
            ////}
            _kernel = new StandardKernel(new ViewModelsModule());
            CreateMain();
            CreateFigureProperties();
            CreateTools();
        }

        #region MainViewModel
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }
        public static void ClearMain()
        {
            _main.Cleanup();
            _main = null;
        }
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = _kernel.Get<MainViewModel>();
            }
        }

        #endregion
        #region FigurePropertiesViewModel
        public static FigurePropertiesViewModel FigurePropertiesStatic
        {
            get
            {
                if (_figureProperties == null)
                {
                    CreateFigureProperties();
                }

                return _figureProperties;
            }
        }
        public FigurePropertiesViewModel FigureProperties
        {
            get
            {
                return FigurePropertiesStatic;
            }
        }
        public static void ClearFigureProperties()
        {
            _figureProperties.Cleanup();
            _figureProperties = null;
        }
        public static void CreateFigureProperties()
        {
            if (_figureProperties == null)
            {
                _figureProperties = _kernel.Get<FigurePropertiesViewModel>();
            }
        }

        #endregion
        #region ToolsViewModel

        public ToolsViewModel Tools
        {
            get { return ToolsStatic; }
        }

        public static ToolsViewModel ToolsStatic
        {
            get
            {
                if (_tools == null)
                {
                    CreateTools();
                }

                return _tools;
            }
        }

        public static void ClearTools()
        {
            _tools.Cleanup();
            _tools = null;
        }

        public static void CreateTools()
        {
            if (_tools == null)
            {
                _tools = _kernel.Get<ToolsViewModel>();
            }
        }

        #endregion
        public static void Cleanup()
        {
            ClearMain();
            ClearFigureProperties();
        }
    }
}